package salonika.demo;

import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xddf.usermodel.chart.*;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFChart;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTDLbls;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTHoleSize;

import java.awt.geom.Rectangle2D;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by feng on 2023/1/5
 */
public class PPTDiagram {

	public static void main(String[] args) throws Exception {
		XMLSlideShow ppt = writePPTByBaseChart();
		FileOutputStream out = new FileOutputStream("C:\\tmp\\merged8.pptx");
		ppt.write(out);
		out.close();

	}

	public static XSSFWorkbook createExcel(){
		// 创建 excel[用于 ppt 图表的基础数据]
		XSSFWorkbook wb = new XSSFWorkbook();

		// 写入数据
		XSSFSheet sheet = wb.createSheet();
		XSSFRow row0 = sheet.createRow(0);
		row0.createCell(1).setCellValue("选项");
		row0.createCell(2).setCellValue("个数");
		for (int i = 0; i < 4; i++) {
			// 设置每一行的字段标题和数据
			XSSFRow row = sheet.createRow(i + 1);
			row.createCell(1).setCellValue(i + 1 + "选项");
			row.createCell(2).setCellValue(i * 5);
		}

		return wb;
	}


	public static XSSFWorkbook readExcel() throws Exception {
		// 创建 excel[用于 ppt 图表的基础数据]

		return new XSSFWorkbook(new FileInputStream("C:\\tmp\\Chart4.xlsx"));
	}


	public static XMLSlideShow writePPTByBaseChart() throws Exception {
		// 创建一个ppt
		XMLSlideShow ppt = new XMLSlideShow();
		//获取excel
		XSSFWorkbook wb = createExcel();

		// 创建了一个幻灯片
		XSLFSlide slide = ppt.createSlide();
		// 创建一个图表
		XSLFChart chartPpt = ppt.createChart();
		// 把工作簿放到图表里，这样可以方便文件更新
		chartPpt.setWorkbook(wb);
		// 图表头
		chartPpt.setTitleText("选中情况环形图");


		// x坐标轴 底部
		XDDFCategoryAxis bottomAxis = chartPpt.createCategoryAxis(AxisPosition.BOTTOM);
		// y轴  左侧
		XDDFValueAxis leftAxis = chartPpt.createValueAxis(AxisPosition.LEFT);

		//创建一个环形图
		XDDFChartData data = chartPpt.createData(ChartTypes.SCATTER, bottomAxis, leftAxis);

		//取刚才 设置的数据
		XSSFSheet sheet = wb.getSheetAt(0);

		// 分类轴标(X轴)数据，单元格范围位置 行：[1, 4] 列：[1, 1]
		XDDFDataSource<String> countries = XDDFDataSourcesFactory.fromStringCellRange(sheet, new CellRangeAddress(1, 4, 1, 1));

		// 设置为可变颜色
		data.setVaryColors(true);

		XDDFNumericalDataSource<Double> area = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 4, 2, 2));

		// 图表加载数据
		data.addSeries(countries, area);

		//画图
		chartPpt.plot(data);


//		CTDLbls dLbls = chartPpt.getCTChart().getPlotArea().getLineChartArray(0).getSerArray(0).addNewDLbls();
//		dLbls.addNewShowCatName().setVal(false);
//		dLbls.addNewShowLeaderLines().setVal(false);
//		dLbls.addNewShowVal().setVal(false);
//		dLbls.addNewShowSerName().setVal(false);
//		dLbls.addNewShowLegendKey().setVal(false);
//		dLbls.addNewShowBubbleSize().setVal(false);
//		dLbls.addNewDelete().setVal(false);
//		dLbls.setSeparator("\n");// 分隔符为分行符


//		CTDLbls dLbls = chartPpt.getCTChart().getPlotArea().getDoughnutChartArray(0).getSerArray(0).addNewDLbls();
//		dLbls.addNewShowVal().setVal(true);//图上面 显示数据
//		dLbls.addNewShowLegendKey().setVal(false);
//		dLbls.addNewShowCatName().setVal(true);//图上面  类别名称
//		dLbls.addNewShowSerName().setVal(false);
//		dLbls.addNewShowPercent().setVal(true);//图上面  显示百分比
//		dLbls.addNewShowLeaderLines().setVal(false);// 引导线
//		dLbls.setSeparator("\n");// 分隔符为分行符
//		dLbls.addNewDLblPos().setVal(org.openxmlformats.schemas.drawingml.x2006.chart.STDLblPos.Enum.forString("inEnd"));// 数据标签内


		//设置环的大小【这里不设置的话  就是个饼图了】
//		CTHoleSize holeSize = chartPpt.getCTChart().getPlotArea().getDoughnutChartArray(0).addNewHoleSize();
//		holeSize.setVal((short) 50);


		//图表位置
		Rectangle2D.Double rect = new Rectangle2D.Double(700000, 500000, 7000000, 5000000);
		// 把图加到幻灯片里，指定画布
		slide.addChart(chartPpt,rect);

		return ppt;
	}
}
